<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="油枪号">
          <a-input placeholder="请输入油枪号" v-decorator="['gunNo', {rules: [{ required: true, message: '请输入油枪号!'}]}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="油品类型">
          <j-dict-select-tag  v-decorator="['oilType', {rules: [{ required: true, message: '请选择油品类型!'}]}]"  @change="getOilNo" placeholder="请选择油品类型"  :triggerChange="true" :type="'radio'"  dictCode="oil_type"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="油号">
          <a-select v-decorator="['oilNo', {rules: [{required: true, message: '请选择油号!'}]}]" placeholder="请选择油号">
            <a-select-option value="">请选择油号</a-select-option>
            <a-select-option  v-for="(item, key) in oilNoOptions" :key="key" :value="item.oilNo">
              {{ item.oilNo }}
            </a-select-option>
          </a-select>
        </a-form-item>
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import { oilNoOptionsList,checkOilGunRepeat } from '@/api/api'

  export default {
    name: "OilGunModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        oilNoOptions: [],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          add: "/lww/oilGun/add",
          edit: "/lww/oilGun/edit",
        },
      }
    },
    created () {
  
    },
    methods: {
      getOilNo(v) {
        this.initOilNoOptions(v);
      },
      initOilNoOptions(oilType){
        oilNoOptionsList({oilType: oilType}).then(res=>{
          if(res.success){
            this.oilNoOptions = res.result
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        if(this.model.hasOwnProperty("id")) {
          this.getOilNo(this.model.oilType);
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'gunNo','oilType','oilNo'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            let params = {
              gunNo: this.model.gunNo,
              id: this.model.id
            }
            checkOilGunRepeat(params).then((res) => {
              if (res.success) {
                httpAction(httpurl,formData,method).then((res)=>{
                  if(res.success){
                    that.$message.success(res.message);
                    that.$emit('ok');
                  }else{
                    that.$message.warning(res.message);
                  }
                }).finally(() => {
                  that.confirmLoading = false;
                  that.close();
                })
              } else {
                this.$message.warning('当前油枪已录入，请勿重复录入!');
                that.confirmLoading = false;
                return false;
              }
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>